package com.zisu.aradvertising.service.impl;

import com.zisu.aradvertising.pojo.Advertiser;
import com.zisu.aradvertising.pojo.AdvertiserSpace;
import com.zisu.aradvertising.pojo.Cooperation;
import com.zisu.aradvertising.pojo.Sign;
import com.zisu.aradvertising.repository.*;
import com.zisu.aradvertising.service.AdvertiserSpaceService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdvertiserSpaceServiceImpl implements AdvertiserSpaceService {
    //实例化抽象类
    @Resource
    private DaoAdvertiserSpace daoAdvertiserSpace;
    @Resource
    private DaoCooperation daoCooperation;
    @Resource
    private DaoAdvertiser daoAdvertiser;
    @Resource
    private DaoSign daoSign;
    @Resource
    private DaoProduct daoProduct;
    @Resource
    private DaoVideo daoVideo;

    @Override
    //实现返回所有广告商和广告位业务逻辑
    public List<AdvertiserSpace> findallbyvidService(int vid) {
        //找出所有的广告位
        List<AdvertiserSpace> advertiserSpaceList= daoAdvertiserSpace.findAllByVid(vid);
        System.out.print(advertiserSpaceList);
        System.out.print("\n");
        List<Advertiser> l1=new ArrayList<>();
        for(AdvertiserSpace x:advertiserSpaceList){
            //找出每个广告位对应的合作信息
            x.setCooperations(daoCooperation.findAllByApid(x.getApid()));
            System.out.print(x.getCooperations());
            System.out.print("\n");
            //找出每个合作对应的广告商
            for(Cooperation c:x.getCooperations())
            {
                Sign sign=daoSign.findByCoid(c.getCoid());
                System.out.print(sign);
                System.out.print("\n");
                c.setAdvertisername(daoAdvertiser.findByAid(sign.getAid()).getAname());
                System.out.print(c);
                System.out.print("\n");
                c.setProductname(daoProduct.findByPid(c.getPid()).getPname());
                System.out.print(c);
                System.out.print("\n");
            }
        }

        return advertiserSpaceList;
    }

    @Override
    public AdvertiserSpace findbyapidService(int apid) {
        AdvertiserSpace advertiserSpace = daoAdvertiserSpace.findByApid(apid);
        advertiserSpace.setVideo(daoVideo.findByVid(advertiserSpace.getVid()));
        return daoAdvertiserSpace.save(advertiserSpace);
    }

    //实现撤下广告位业务逻辑
    @Override
    public AdvertiserSpace quashService(int apid) {
        AdvertiserSpace advertiserSpace = daoAdvertiserSpace.findByApid(apid);
        advertiserSpace.setApsumcoreaption(-1);
        return daoAdvertiserSpace.save(advertiserSpace);
    }

}
